package com.bw.android.bwgao6week2a.fragment;

import static android.app.Activity.RESULT_OK;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.UriUtils;
import com.bw.android.bwgao6week2a.AppInstance;
import com.bw.android.bwgao6week2a.R;
import com.bw.android.bwgao6week2a.databinding.FragmentAuthBinding;
import com.bw.android.bwgao6week2a.vm.AuthRealNameViewModel;
import com.bw.android.network.RequestClient;
import com.bw.ch.baselib.BaseBindingFragment;

import java.io.File;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class AuthFragment extends BaseBindingFragment<FragmentAuthBinding> {
    private AuthRealNameViewModel viewModel;
    public AuthFragment() {
        super(true);
    }

    @Override
    public int getLayout() {
        return R.layout.fragment_auth;
    }

    @Override
    public void initView() {
        mBinding.setLifecycleOwner(this);
        viewModel = new ViewModelProvider(this).get(AuthRealNameViewModel.class);
        mBinding.setAuthRealNameVm(viewModel);
        mBinding.btnPickIdCard.setOnClickListener(v -> {
            //打开系统相册选择封面
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent,1);
        });
        //监听是否选定封面图片
        viewModel.idCardFile.observe(this, file -> {
            Bitmap bitmap = ImageUtils.getBitmap(file);
            mBinding.ivIdCard.setImageBitmap(bitmap);
            RequestClient.buildService().authRealName(
                    AppInstance.getInstance().getUserId(),
                    AppInstance.getInstance().loginResultBean.getSessionId(),
                    "张三",
                    1,
                    "2002-08-01",
                    "北京市海淀区马连洼路",
                    "110102199801230125",
                    "北京海淀分局",
                    "2030-07-31"
            ).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(statusBean -> {
                Toast.makeText(AuthFragment.this.getContext(), statusBean.message, Toast.LENGTH_LONG).show();
            }, throwable -> {
                Toast.makeText(AuthFragment.this.getContext(), throwable.toString(), Toast.LENGTH_LONG).show();
            });
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK){
            //打系统开相并选取图片后，设置封面并保存封面文件，用于后续上传封面
            File file = UriUtils.uri2File(data.getData());
            //保存封面文件
            viewModel.idCardFile.setValue(file);
        }
    }
}